package com.liu.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liu.eduservice.entity.EduChapter;
import com.liu.eduservice.entity.EduVideo;
import com.liu.eduservice.entity.chapter.ChapterVo;
import com.liu.eduservice.entity.chapter.VideoVo;
import com.liu.eduservice.mapper.EduChapterMapper;
import com.liu.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.eduservice.service.EduVideoService;
import com.liu.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author achang
 * @since 2022-05-20
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    EduVideoService eduVideoService;


    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {

        ArrayList<ChapterVo> finalChapterVos = new ArrayList<>();

        // 1.查询所有章节数据
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        // 查询的的章节数据，由于当前写的方法在EduChapterServiceImpl下，所以可以直接写this或者baseMapper
        List<EduChapter> eduChapters = baseMapper.selectList(wrapper);

        // 2.查询所有小节数据
        QueryWrapper<EduVideo> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("course_id", courseId);
        // 查询的的章节数据，由于当前写的方法在EduChapterServiceImpl下，所以不可以直接写this或者baseMapper
        List<EduVideo> eduVideos = eduVideoService.list(wrapper1);

        // 3.遍历所有章节list,进行数据封装
        for (int i = 0; i < eduChapters.size(); i++) {
            EduChapter chapter = eduChapters.get(i);

            //创建章节vo对象
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(chapter, chapterVo);
            finalChapterVos.add(chapterVo);

            //填充课时vo对象
            ArrayList<VideoVo> finalVideoVos = new ArrayList<>();
            // 4.遍历所有小节
            for (int j = 0; j < eduVideos.size(); j++) {
                EduVideo video = eduVideos.get(j);

                // 4.2 判断小节表中chapterId和章节表的id做判断
                if (chapter.getId().equals(video.getChapterId())) {
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(video, videoVo);
                    finalVideoVos.add(videoVo);
                }
            }
            // 5.将小节finalVideoList 封装到章节对象中
            chapterVo.setChildren(finalVideoVos);

        }
        return finalChapterVos;

    }

    @Override
    public boolean deleteChapter(String chapterId) {
        // 根据chapter章节id 查询查询小节表，如果查询有数据，则不删除
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id", chapterId);
        int count = eduVideoService.count(queryWrapper);
        // 判断是否查询出数据
        if (count > 0) {
            throw new GuliException(20001, "还有小节数据，不能删除");
        } else {
            // 不能查询出小节，进行删除
            int byId = baseMapper.deleteById(chapterId);
            return byId > 0;
        }
    }

    // 根据课程id删除章节
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        try {
            int delete = baseMapper.delete(queryWrapper);
            if (delete <= 0) {
                throw new GuliException(20001, "删除课程章节失败！");
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

}
